package com.lianqi.emcpframework.design.domain;

import java.io.*;

/**
 * @program emcp-framework
 * @ClassName com.lianqi.emcpframework.design.domain.ValueObject
 * @description: 领域值对象定义
 * @author: sky
 * @create: 2018/11/21 11:28
 */
public interface ValueObject<T> {

    /**
     * 通过属性对比 确定是否是同一个值对象
     * 
     * @param other
     * @return
     */
    boolean sameValueAs(T other);

    /**
     * 对象拷贝 默认实现了深度拷贝
     * 
     * @return
     */
    default T copy() {
        try {
            // 将对象写到流里
            ByteArrayOutputStream bo = new ByteArrayOutputStream();
            ObjectOutputStream oo = new ObjectOutputStream(bo);
            oo.writeObject(this);
            // 从流里读出来
            ByteArrayInputStream bi = new ByteArrayInputStream(bo.toByteArray());
            ObjectInputStream oi = new ObjectInputStream(bi);
            return ((T)oi.readObject());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return null;
    }
}
